<?php

/**
 * @filename IndexController.php
 * @author lengdabao <lengdb@fastweb.com.cn>
 * @datetime 2016/5/20 14:58
 * @description
 */
class Member_IndexController extends SellerController
{
    public function indexAction()
    {
    	$sellerId = $this->getCurrentUser()->getSellerId();
    	$shopId = $this->getCurrentUser()->getShopId();
        if (!$shopId) {
            $this->redirect('/index/chose-shop');
        }
    	$userGeneral = SellerMemberStatService::readGeneral($sellerId,$shopId);
    	$userDaily = SellerMemberStatService::readDaily($sellerId,$shopId);
        $orderGeneral = SellerOrderStatService::readGeneral($sellerId,$shopId);
        $orderDaily = SellerOrderStatService::readDaily($sellerId,$shopId);
        $tenDayConsumptionUser = SellerMemberStatService::tenDayConsumptionUser($sellerId,$shopId);
        $tenDayMember = SellerMemberStatService::tenDayMember($sellerId,$shopId);
        $userProportion = SellerMemberStatService::getUserProportion($sellerId,$shopId);
        $memberProportion = SellerMemberStatService::getMemberProportion($sellerId,$shopId);
        $genderProportion = SellerMemberStatService::getGenderProportion($sellerId,$shopId);
        
        //查询今天以后7天的生日人数
        $start = date('m-d');
        $end = date('m-d',strtotime("+6 day"));
        
        $userFansModel = new U_FansModel($sellerId);
        $where['seller_id'] = $sellerId;
        $res = $userFansModel->find("seller_id=$sellerId AND FROM_UNIXTIME(`birthday`,'%m-%d') BETWEEN $start AND $end",array('COUNT(`id`) as count'));
        $userDaily[0]['seven_day_birthday_member_num'] = $res;
        
        //会员卡，按等级从低到高取前三名
        $cardList = User_CardService::getListBySellerId($sellerId);
        $card = array();
        if(!empty($cardList))
        {
        	foreach ($cardList as $k=>$v)
        	{
        		if($k <=2)
        		{
        			if(!empty($v['pic_id']))
        			{
        				$picModel = new PicDataModel();
        				$picInfo = $picModel->getInfoById($v['pic_id']);
        				$v['org'] = $picInfo['org'];
        			}
        			else 
        			{
        				$v['org'] = '/www/seller/images/common/default_banner_pic.png';
        			}
        			$card[] = $v;
        		}
        	}
        }
        $data['birthday_start'] = $start;
        $data['birthday_end'] = $end;
        
        $data['card'] = $card;
    	$data['user_general'] = $userGeneral[0];
    	$data['user_daily'] = $userDaily[0];
    	$data['order_general'] = $orderGeneral[0];
    	$data['order_daily'] = $orderDaily[0];
        $data['consumption_user_data'] = json_encode($tenDayConsumptionUser);
        $data['member_data'] = json_encode($tenDayMember);
        $data['user_proportion'] = json_encode($userProportion);
        $data['member_proportion'] = json_encode($memberProportion);
        $data['gender_proportion'] = json_encode($genderProportion);
        $data['loadModuleScript'] = true;
        $this->display($data, 'member/index/index.html');
    }
}